package com.study.springboot.service.impl;

import com.study.springboot.model.entity.Student;
import com.study.springboot.model.vo.*;
import com.study.springboot.service.StudentService;
import com.study.springboot.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.util.List;

/**
 * @author wei.zhao
 * @description:
 * @date 2021年12月04日 23:33
 */
@Service
@PropertySource(value = "classpath:default.properties", encoding = "UTF-8")
public class TestServiceImpl implements TestService {

    @Autowired
    MainDefaultStudent mainDefaultStudent;

    @Autowired
    MainDefaultStudents mainDefaultStudents;

    @Value("${student.name}")
    private String defaultName;

    @Value("${student.hobbies}") // 默认会按照","分割
    private List<String> hobbies;

    @Value("#{'${student.hobbies}'.split(',')}") // 调函数
    private List<String> hobbies02;

    @Value("#{T(java.lang.System).currentTimeMillis()}") // 执行系统函数
    private Long curTime;

    @Autowired
    DefaultStudent defaultStudent;

    @Autowired
    Defaults defaults;

    @Autowired
    DefaultStudents defaultStudents;

    @Autowired
    TransactionManager transactionManager;

    @Autowired
    DataSource dataSource;

    @Autowired
    StudentService studentService;

    @Override
    public void testReadProfiles() {
        System.out.println("测试读取主配置文件对象属性：" + mainDefaultStudent);
        System.out.println("测试读取主配置文件对象集合属性：" + mainDefaultStudents);
        System.out.println("测试读取属性：" + defaultName);
        System.out.println("测试读取对象属性：" + defaultStudent);
        System.out.println("测试读取集合属性：" + defaults);
        System.out.println("测试读取对象集合属性：" + defaultStudents);
        System.out.println(hobbies.get(0));
        System.out.println(hobbies02.get(0));
        System.out.println(curTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testTransaction() {
        studentService.save(Student.builder().name("测试事务").age(20).build());
        int i = 1 / 0;
    }

}
